package com.collect.zambia.zhiScene.zhiCollection.zhiBigGroup.zhiRoot;

import android.annotation.SuppressLint;
import android.text.TextUtils;

import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.collect.zambia.R;
import com.collect.zambia.zhiBase.BaseActivity;
import com.collect.zambia.zhiBean.zhiSend.ZHISDailyCaseEntity;
import com.collect.zambia.zhiBean.zhiSend.ZHISPerfermanceEntity;
import com.collect.zambia.zhiGlobal.zhiAppSettings.ZHIProjectRouter;
import com.collect.zambia.zhiHelper.ZHIMainProcessHelper;
import com.collect.zambia.zhiScene.zhiCollection.zhiBigGroup.zhiAccount.ZHIAccountBigGroupFragment;
import com.collect.zambia.zhiScene.zhiCollection.zhiBigGroup.zhiPerformance.ZHIPerformanceBigGroupOrderFragment;
import com.collect.zambia.zhiScene.zhiWidget.zhiFilterDaily.ZHIFilterDaily;
import com.collect.zambia.zhiScene.zhiWidget.zhiFilterDaily.ZHIFilterDailyClickListener;
import com.collect.zambia.zhiScene.zhiWidget.zhiNavigationBar.ZHIBottomTabBarItem;

import butterknife.BindView;

@SuppressLint("NonConstantResourceId")
@Route(path = ZHIProjectRouter.ZHI_ROOT_BIG_GROUP)
public class ZHIRootBigGroupActivity extends BaseActivity implements ZHIRootBigGroupFragmentInterface {

    @BindView(R.id.drawer_layout)
    DrawerLayout drawer_layout;

    @BindView(R.id.drawer_filter_daily)
    ZHIFilterDaily drawer_filter_daily;

    @BindView(R.id.bar_account)
    ZHIBottomTabBarItem bar_account;
    @BindView(R.id.bar_perfermance)
    ZHIBottomTabBarItem bar_perfermance;

    private FragmentManager mFragmentManager;
    private ZHIAccountBigGroupFragment mZHIAccountBigGroupFragment;

    private ZHIPerformanceBigGroupOrderFragment mZHIPerformanceBigGroupOrderFragment;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_root_big_group;
    }

    @Override
    protected void initInjector() {
    }

    @Override
    protected void initView() {
        try {
            mFragmentManager = getSupportFragmentManager();

            drawer_filter_daily.setZHIFilterDailyClickListener(new ZHIFilterDailyClickListener() {
                @Override
                public void onConfirmClick() {
                    try {
                        if (drawer_layout.isDrawerOpen(drawer_filter_daily)) {
                            drawer_layout.closeDrawer(drawer_filter_daily);
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onCloseClick() {
                    if (drawer_layout.isDrawerOpen(drawer_filter_daily)) {
                        drawer_layout.closeDrawer(drawer_filter_daily);
                    }
                }
            });
            drawer_filter_daily.setDefaultMember(ZHIMainProcessHelper.getInstance().getLoginEntity().getUserName());

//            drawer_filter_performance.setZHIPerformanceFilterClickListener(new ZHIFilterPerformanceClickListener() {
//                @Override
//                public void onConfirmClick() {
//                    if (drawer_layout.isDrawerOpen(drawer_filter_performance)) {
//                        drawer_layout.closeDrawer(drawer_filter_performance);
//                    }
//
//                    if (CFMainProcessHelper.getInstance().getLoginEntity().isLevelBPlus()) {
//                        if (mZHIPerformanceBigGroupAmountFragment != null) {
//                            mZHIPerformanceBigGroupAmountFragment.refreshData("");
//                        }
//                        return;
//                    }
//
//                    if (mZHIPerformanceBigGroupOrderFragment != null) {
//                        mZHIPerformanceBigGroupOrderFragment.refreshData("");
//                    }
//                }
//
//                @Override
//                public void onCloseClick() {
//                    if (drawer_layout.isDrawerOpen(drawer_filter_performance)) {
//                        drawer_layout.closeDrawer(drawer_filter_performance);
//                    }
//                }
//            });

            initEvent();
            selecFragment(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onReloadGroupUserList() {
//        if (drawer_filter_performance != null) {
//            drawer_filter_performance.updateUserDataSource();
//        }
    }

    @Override
    public void onShowFilterDaily() {
        if (drawer_layout.isDrawerOpen(drawer_filter_daily)) {
            drawer_layout.closeDrawer(drawer_filter_daily);
        } else {
            drawer_layout.openDrawer(drawer_filter_daily);
        }
    }

    @Override
    public void onShowFilterPerformanceAmount() {
//        if (drawer_filter_performance != null) {
//            drawer_filter_performance.setToTypeMoney();
//            if (drawer_layout.isDrawerOpen(drawer_filter_performance)) {
//                drawer_layout.closeDrawer(drawer_filter_performance);
//            } else {
//                drawer_layout.openDrawer(drawer_filter_performance);
//            }
//        }
    }

    @Override
    public void onShowFilterPerformanceOrder() {
//        if (drawer_filter_performance != null) {
//            drawer_filter_performance.setToTypeOrder();
//            if (drawer_layout.isDrawerOpen(drawer_filter_performance)) {
//                drawer_layout.closeDrawer(drawer_filter_performance);
//            } else {
//                drawer_layout.openDrawer(drawer_filter_performance);
//            }
//        }
    }

    @Override
    public ZHISPerfermanceEntity onGetPerfermanceFilterParams() {
        ZHISPerfermanceEntity zhisPerfermanceEntity = new ZHISPerfermanceEntity();
//        zhisPerfermanceEntity.setLeven(drawer_filter_performance.getLevel());
//        zhisPerfermanceEntity.setEndDate(drawer_filter_performance.getDueEndDate());
//        zhisPerfermanceEntity.setStartDate(drawer_filter_performance.getDudStartDate());
//        zhisPerfermanceEntity.setLeaderUserId(drawer_filter_performance.getMember());
        return zhisPerfermanceEntity;
    }

    @Override
    public ZHISPerfermanceEntity onGetPerfermanceFilterAmountParams() {
        ZHISPerfermanceEntity zhisPerfermanceEntity = new ZHISPerfermanceEntity();
//        zhisPerfermanceEntity.setLeven(drawer_filter_performance.getLevel());
//        zhisPerfermanceEntity.setEndDate(drawer_filter_performance.getDueEndDate());
//        zhisPerfermanceEntity.setStartDate(drawer_filter_performance.getDudStartDate());
//        zhisPerfermanceEntity.setLeaderUserId(drawer_filter_performance.getMember());
        return zhisPerfermanceEntity;
    }

    @Override
    public ZHISPerfermanceEntity onGetPerfermanceFilterOrderParams() {
        ZHISPerfermanceEntity zhisPerfermanceEntity = new ZHISPerfermanceEntity();
//        zhisPerfermanceEntity.setLeven(drawer_filter_performance.getLevel());
//        zhisPerfermanceEntity.setEndDate(drawer_filter_performance.getDueEndDate());
//        zhisPerfermanceEntity.setStartDate(drawer_filter_performance.getDudStartDate());
//        zhisPerfermanceEntity.setLeaderUserId(drawer_filter_performance.getMember());
        return zhisPerfermanceEntity;
    }

    @Override
    public ZHISDailyCaseEntity onGetDailyFilterParams() {
        ZHISDailyCaseEntity zhisDailyCaseEntity = new ZHISDailyCaseEntity();
        zhisDailyCaseEntity.setTypeOne(drawer_filter_daily.getOrderType());
        zhisDailyCaseEntity.setTypeTwo(drawer_filter_daily.getCallType());
        zhisDailyCaseEntity.setContactResult(drawer_filter_daily.getCallResult());
        zhisDailyCaseEntity.setPtpTimeStart(drawer_filter_daily.getPromisedRepayStartDate());
        zhisDailyCaseEntity.setPtpTimeEnd(drawer_filter_daily.getPromisedRepayEndDate());
        zhisDailyCaseEntity.setRepayStartTime(drawer_filter_daily.getDudStartDate());
        zhisDailyCaseEntity.setRepayEndTime(drawer_filter_daily.getDueEndDate());
        zhisDailyCaseEntity.setCalcType(drawer_filter_daily.getCompareFlag());
        zhisDailyCaseEntity.setOverdueDay(drawer_filter_daily.getCompareOverdueDays());
        zhisDailyCaseEntity.setMobile(drawer_filter_daily.getUserMobile());
        zhisDailyCaseEntity.setPartRepayFlag(drawer_filter_daily.getPartRepay());

        //如果不选择成员，不用赋值，使用默认
        if (!TextUtils.isEmpty(drawer_filter_daily.getMember())) {
            zhisDailyCaseEntity.setSysLoginName(drawer_filter_daily.getMember());
        }

        return zhisDailyCaseEntity;
    }

    public void initEvent() {
        bar_perfermance.setCFOnMenuItemDidChangedListener(item -> {
            bar_account.setChecked(false);
            selecFragment(0);
        });
        bar_account.setCFOnMenuItemDidChangedListener(item -> {
            bar_perfermance.setChecked(false);
            selecFragment(1);
        });
    }

    private void selecFragment(int indext) {
        FragmentTransaction fragemntTransaction = mFragmentManager.beginTransaction();
        hideFragment(fragemntTransaction);
        switch (indext) {
            case 0: {
                bar_perfermance.setChecked(true);
                bar_account.setChecked(false);
                if (null == mZHIPerformanceBigGroupOrderFragment) {
                    mZHIPerformanceBigGroupOrderFragment = new ZHIPerformanceBigGroupOrderFragment();
                    fragemntTransaction.add(R.id.content, mZHIPerformanceBigGroupOrderFragment);
                } else {
                    fragemntTransaction.show(mZHIPerformanceBigGroupOrderFragment);
                }
                break;
            }
            case 1: {
                bar_perfermance.setChecked(false);
                bar_account.setChecked(true);
                if (null == mZHIAccountBigGroupFragment) {
                    mZHIAccountBigGroupFragment = new ZHIAccountBigGroupFragment();
                    fragemntTransaction.add(R.id.content, mZHIAccountBigGroupFragment);
                } else {
                    fragemntTransaction.show(mZHIAccountBigGroupFragment);
                }
                break;
            }
            default:
                break;
        }
        fragemntTransaction.commit();
    }

    private void hideFragment(FragmentTransaction fragemntTransaction) {
        if (fragemntTransaction == null) return;

        if (null != mZHIAccountBigGroupFragment) {
            fragemntTransaction.hide(mZHIAccountBigGroupFragment);
        }

        if (null != mZHIPerformanceBigGroupOrderFragment) {
            fragemntTransaction.hide(mZHIPerformanceBigGroupOrderFragment);
        }
    }
}
